new Vue({
    el: '#vue',
    data: {
		action: getRequestValue('action'),
		id: getRequestValue('id'),
        sysData: []
    },
    filters: {
    },
    mounted: function () {
    	if(this.action != 'insert') {
        	this.initList();
    	}
        this.validate();
        this.menuList();
    },
    methods: {
        initList: function () {
            var that = this;

            _AJAX({
                url: AJAX_URL_USER + 'api/sys/sysRole/select/'+that.id,
				type: 'get',
                success: function (data) {
                    that.sysData = data.data;
                }
            });
        },
        menuList: function() {
            var that = this;
			_AJAX({
				url: AJAX_URL_USER + 'api/sys/sysMenu/list',
				type: 'get',
				success: function(data) {
					$("#menuTree").jstree({
        				"checkbox": {"keep_selected_style": false},
						"core": {
				            "multiple": true,
				            "animation": 0,
							"themes": {
								theme: "classic",
								"dots": true,
								"icons": true
							},
							"check_callback": true,
							'data': data.data
						},
						"plugins": ["wholerow", "checkbox"],
						"checkbox" : {
					    	'three_state' : false,     
					    	'cascade' : 'undetermined'
	              		}
					}).on("loaded.jstree", function (event, data) {
						$('#menuTree').jstree('select_node', that.sysData.menuList, true);
					});
				}
			});
        },
        selectMenuId: function() {
	        var array = $.jstree.reference('#menuTree').get_selected(true);
	        var set = new Set();
	        if (array) {
	            for (var i = 0; i < array.length; i++) {
	                var a = array[i]
	                set.add(a.id);
	                if (a.parent !== "#") {
	                    set.add(a.parent);
	                }
	            }
	        }
			return Array.from(set);
        },
        validate: function() {
            var that = this;
			var form = $('#slifeForm');
			form.validate({
				errorElement: 'span',
				focusInvalid: true,
				messages: {
				},
				rules: {
					name: {
						maxlength: 64,
						required: true
					},
					state: {
						required: true
					}
				},
				submitHandler: function(form) {
					that.submitData();
				}
			});
        },
        submitData: function() {
            var that = this;
            var url = "";

            var data = {
				name: that.sysData.name,
				state: that.sysData.state,
				menuList: that.selectMenuId()
            }
            
            if(that.action == "insert") {
            	url = AJAX_URL_USER + 'api/sys/sysRole/insert';
            }
            if(that.action == "update") {
            	url = AJAX_URL_USER + 'api/sys/sysRole/update';
            	data.id = that.id;
            }
            
			_AJAX({
				url: url,
				type: 'post',
				data: JSON.stringify(data),
				success: function(data) {
	    			var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				}
			});
        }
    }
});




